import os
from time import sleep

import allure
import pytest

import page
from page.page_conversation_bottom_btn import PageConversationBot
from tool.get_driver import GetDriver
from tool.get_log import GetLogger
from tool.read_json import read_json_paras

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("群内功能")
@allure.feature("底部开关功能测试")
class TestConversationBot():

    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.con_bot = PageConversationBot(cls.driver)
        cls.con_bot.page_init()
        cls.con_bot.page_conversation_entrance()
        log.info("正在自动生成霸屏/打赏测试用例")
        cls.con_bot.page_conversation_init_baping_paras()
        cls.con_bot.page_conversation_init_dashang_paras()
        log.info("霸屏/打赏测试用例生成完毕")
        # 调试05用的测试数据
        # cls.song_name = ['女孩（ BOOTLEG MIX）', '黑桃A', '雪 Distance']

    @classmethod
    def teardown_class(cls):
        input("结束")
        GetDriver.quit_app_driver()

    @allure.title("霸屏测试")
    @pytest.mark.parametrize("sub_loc, sub_name, time_loc, time, msg", read_json_paras("baping_paras.json"))
    def test01_conversation_bot_baping(self, sub_loc, sub_name, time_loc, time, msg):
        with allure.step('1.点击霸屏按钮'):
            pass
        self.con_bot.page_conversation_click_baping()
        with allure.step('2.根据设计的测试用例发送霸屏'):
            pass
        self.con_bot.page_conversation_send_baping(sub_loc, time_loc, msg)
        try:
            with allure.step('3.断言：霸屏卡片是否发送成功'):
                pass
            sleep(5)
            assert self.con_bot.page_conversation_get_baping_msg() in msg
            assert self.con_bot.page_conversation_get_baping_time() == time
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except AssertionError:
            self.con_bot.base_get_screenshot_as_file(module_name, '霸屏卡片发送失败')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversation_back_url()

    @allure.title("带你飞1测试")
    def test02_conversation_bot_dainifei1(self):
        with allure.step('1.点击带你飞按钮'):
            pass
        self.con_bot.page_conversation_click_dainifei()
        with allure.step('2.配置参数并发送带你飞'):
            pass
        self.con_bot.page_conversition_send_dainifei1()
        try:
            with allure.step('3.断言：带你飞卡片是否发送成功'):
                pass
            sleep(5)
            assert self.con_bot.page_conversation_get_dainifei_time() == "30"
            assert self.con_bot.page_conversation_get_dainifei_msg() in "daoqitiaozhanzhe"
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.con_bot.base_get_screenshot_as_file(module_name, '带你飞卡片发送失败')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversation_back_url()

    @allure.title("带你飞2测试")
    def test03_conversation_bot_dainifei2(self):
        with allure.step('1.点击带你飞按钮'):
            pass
        self.con_bot.page_conversation_click_dainifei()
        with allure.step('2.配置参数并发送带你飞'):
            pass
        self.con_bot.page_conversition_send_dainifei2()
        try:
            with allure.step('3.断言：带你飞卡片是否发送成功'):
                pass
            sleep(5)
            assert self.con_bot.page_conversation_get_dainifei_time() == "60"
            assert self.con_bot.page_conversation_get_dainifei_msg() in "lanbojini"
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.con_bot.base_get_screenshot_as_file(module_name, '带你飞卡片发送失败')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversation_back_url()

    @allure.title("点歌测试")
    def test04_conversation_bot_song(self):
        with allure.step('1.点击点歌按钮'):
            pass
        self.con_bot.page_conversation_click_baping_song()
        with allure.step('2.选择歌曲'):
            pass
        song_name = self.con_bot.page_conversation_select_song()
        self.save_song_name(song_name)
        with allure.step(f'  当前选择的歌曲为{song_name}\n3.点击发送按钮并配置发送参数'):
            pass
        self.con_bot.page_conversation_send_song()
        try:
            with allure.step('4.断言：点歌卡片是否发送成功'):
                pass
            sleep(5)
            assert set(self.con_bot.page_conversation_song_card_name()) == set(song_name)
            assert self.con_bot.page_conversation_song_card_num() == "3首"
            assert self.con_bot.page_conversation_song_card_msg() in "testsongmsg"
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.con_bot.base_get_screenshot_as_file(module_name, '点歌卡片发送失败')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversation_back_url()

    @classmethod
    def save_song_name(cls, song_name):
        cls.song_name = song_name

    @allure.title("已点歌曲测试")
    def test05_conversation_bot_song_selected(self):
        # ['女孩（ BOOTLEG MIX）', '黑桃A', '雪 Distance']
        with allure.step('1.点击点歌按钮'):
            pass
        self.con_bot.page_conversation_click_baping_song()
        with allure.step('2.点击已点歌单'):
            pass
        self.con_bot.page_conversation_click_song_selected_tab()
        try:
            with allure.step('3.断言：已点歌曲是否在列表中'):
                pass
            assert set(self.con_bot.page_conversation_get_selected_names()) == set(self.song_name)
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.con_bot.base_get_screenshot_as_file(module_name, '已点歌单中未包含已点歌曲')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversrion_song_back_to_chat()
            self.con_bot.page_conversation_back_url()

    @allure.title("打赏测试")
    @pytest.mark.parametrize("sub_loc, sub_name, obj_loc, obj, msg", read_json_paras("dashang_paras.json"))
    def test06_conversation_bot_dashang(self, sub_loc, sub_name, obj_loc, obj, msg):
        with allure.step('1.点击打赏按钮'):
            pass
        self.con_bot.page_conversation_click_dashang()
        with allure.step('2.根据设计的测试用例发送打赏'):
            pass
        self.con_bot.page_conversation_send_dashang(sub_loc, obj_loc, msg)
        try:
            with allure.step('3.断言：打赏卡片是否发送成功'):
                pass
            sleep(5)
            assert self.con_bot.page_conversation_get_dashang_gift() == sub_name
            assert self.con_bot.page_conversation_get_dashang_msg() in msg
            assert self.con_bot.page_conversation_get_dashang_obj() == obj
            log.info('断言成功')
            self.con_bot.base_get_screenshot_as_file(module_name, '断言成功截图')
        except AssertionError:
            self.con_bot.base_get_screenshot_as_file(module_name, '霸屏卡片发送失败')
            log.error('断言失败')
            raise
        finally:
            self.con_bot.page_conversation_back_url()